package com.cmcc.waf.report.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.cmcc.waf.report.feign.model.QmThreatHostTopResponse;
import com.cmcc.waf.report.feign.model.ResponseVO;
import com.cmcc.waf.report.feign.model.ThreatTypeVO;
import com.cmcc.waf.report.feign.model.WebSafeRequest;
import com.cmcc.waf.report.request.ThreatTypeRequest;
import com.cmcc.waf.report.service.ThreatTypeService;
import com.cmcc.waf.report.utils.EcloudResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author liang_jinxin
 * @date 2025/1/15 15:37
 * 接口文档：<a href="https://ecloud.10086.cn/op-help-center/doc/article/90352">业务总览-web安全防护-大屏-攻击类型分布</a>
 */

@RestController
@Slf4j
public class ThreatTypeController {

    @Autowired
    private ThreatTypeService threatTypeService;

    @PostMapping("attack-type")
    public EcloudResult<List<ThreatTypeVO>> threatType(@RequestBody ThreatTypeRequest request) {
        request.setStartTime(StrUtil.isEmpty(request.getStartTime()) ? DateUtil.beginOfDay(DateUtil.date()).toString() : request.getStartTime());
        request.setEndTime(StrUtil.isEmpty(request.getEndTime()) ? DateUtil.now() : request.getEndTime());
        ResponseVO<List<ThreatTypeVO>> response = threatTypeService.threatType(request);
        if (response.success()) {
            return new EcloudResult<List<ThreatTypeVO>>().ok(response.getBody());
        } else {
            return new EcloudResult<List<ThreatTypeVO>>().error(Integer.parseInt(response.getCode()), response.getMessage());
        }
    }

    /**
     * 威胁主机TOP5
     * @param request
     * @return
     */
    @PostMapping("attack-asse/top")
    public EcloudResult<List<QmThreatHostTopResponse>> threatHostTop(@RequestBody WebSafeRequest request) {
        request.setStartTime(StrUtil.isEmpty(request.getStartTime()) ? DateUtil.beginOfDay(DateUtil.date()).toString() : request.getStartTime());
        request.setEndTime(StrUtil.isEmpty(request.getEndTime()) ? DateUtil.now() : request.getEndTime());
        ResponseVO<List<QmThreatHostTopResponse>> response = threatTypeService.threatHostTop(request);
        if (response.success()) {
            return new EcloudResult<List<QmThreatHostTopResponse>>().ok(response.getBody());
        } else {
            return new EcloudResult<List<QmThreatHostTopResponse>>().error(Integer.parseInt(response.getCode()), response.getMessage());
        }
    }

}
